
//  WFChannelDetailViewController.m
//  FAKER
//
//  Created by Jianwei on 15/11/7.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "WFChannelDetailViewController.h"
#import "AllHeader.h"
#import "FitHeader.h"
#import "BaseClassCollectionView.h"
#import "WFChannelDetailCollectionViewCell.h"
#import "WFChannelDetailModel.h"
#import "WFModelSingleton.h"
#import "WFChannelWebViewController.h"
#define WFUrlString @"http://weibo.com/u/5290447258"
@interface WFChannelDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, WFChannelDetailCollectionViewCellDelegate, UIViewControllerPreviewingDelegate>
@property(nonatomic, strong)BaseClassCollectionView *backCollectionView;
@property(nonatomic, strong)NSMutableArray *dataSourceArray;
@property(nonatomic, assign)NSInteger index;
@property(nonatomic, strong)YHLoadingView *loadingView;
@property(nonatomic, strong)YHBottomView *bottomView;
@end

@implementation WFChannelDetailViewController
- (void)pushWFchannelWebViewControllerWithModel:(WFChannelDetailModel *)model
{
    WFChannelWebViewController *wc = [[WFChannelWebViewController alloc]init];
    wc.model = model;
    [self.navigationController pushViewController:wc animated:1];
}
- (void)viewWillAppear:(BOOL)animated
{
//    [[UIApplication sharedApplication] setStatusBarStyle:0 animated:YES];
//    [UIApplication sharedApplication].statusBarStyle = NSUSERDEFAULTS_NIGHT;
    self.navigationController.navigationBar.hidden = 1;
}
- (void)viewWillDisappear:(BOOL)animated
{
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
    
    
//    [self.navigationController setToolbarHidden:YES animated:NO];
}
- (void)viewDidAppear:(BOOL)animated
{
    [UIApplication sharedApplication].statusBarStyle = NSUSERDEFAULTS_NIGHT;
}
- (void)getData
{
    [InternetDataRequest getDataByString:_api_url BodyDic:nil WithDataBlock:^(id data) {
        [_loadingView removeFromSuperview];
        
        WFChannelDetailInfoModel *model = [[WFChannelDetailInfoModel alloc]init];
        [model setValuesForKeysWithDictionary:[data objectForKey:@"data"]];
        [WFModelSingleton shareInstance].model = model;
        
        NSMutableArray *array = [[data objectForKey:@"data"] objectForKey:@"articles"];
        NSMutableArray *arr = [NSMutableArray arrayWithArray:array];
        if(array.count % 6 != 0)
        {
            NSInteger l = 36 - array.count;
            for (NSInteger i = 0; i < l; i ++) {
                NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjects:@[@"王飞", @"专业iOS开发三十年"] forKeys:@[@"auther_name", @"title"]];
                [arr insertObject:dic atIndex:arc4random() % (arr.count + 1 + i)];
            }
        }
        
        for (NSInteger i = 0; i < 6; i ++) {
            NSMutableArray *dataArray = [NSMutableArray array];
            for (NSInteger j = 0; j < 6; j ++) {
                WFChannelDetailModel *model = [[WFChannelDetailModel alloc]init];
                [model setValuesForKeysWithDictionary:arr[i * 6 + j]];
                [dataArray addObject:model];
            }
            for (NSInteger k = 0; k < 6; k ++) {
                if ([[dataArray[k] media] count] != 0) {
                    [dataArray exchangeObjectAtIndex:k withObjectAtIndex:0];
                    break;
                }
                
            }
                    [_dataSourceArray addObject:dataArray];
        }
        [_backCollectionView reloadData];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //创建加载中视图
    _loadingView = [[YHLoadingView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 40)];
    [self.view addSubview:_loadingView];
}
- (void)loadView
{
    [super loadView];
    _dataSourceArray = [NSMutableArray array];
    
    _index = 0;
    [self getData];
    [self registerForPreviewingWithDelegate:self sourceView:self.view];
    
    
    UICollectionViewFlowLayout *backCollectionViewFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    backCollectionViewFlowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 140);
    backCollectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(80, 0, 0, 0);
    backCollectionViewFlowLayout.minimumInteritemSpacing = 0;
    backCollectionViewFlowLayout.minimumLineSpacing = 0;
    backCollectionViewFlowLayout.scrollDirection = 1;
    
    
    _backCollectionView = [[BaseClassCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 40) collectionViewLayout:backCollectionViewFlowLayout];
    _backCollectionView.pagingEnabled = 1;
    _backCollectionView.dataSource = self;
    _backCollectionView.delegate = self;
    [_backCollectionView registerClass:[WFChannelDetailCollectionViewCell class] forCellWithReuseIdentifier:@"back"];
    [self.view addSubview:_backCollectionView];
    
    
    
    
    //创建底部条
    _bottomView = [[YHBottomView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 40, SCREEN_WIDTH, 40)];
    _bottomView.refreshButton = nil;
    void (^bottomButtonBlock)(NSString *) = ^(NSString *buttonTitle) {
        if ([buttonTitle isEqualToString:@"back"]) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        else {
            //修改夜间模式
            if (NSUSERDEFAULTS_NIGHT) {
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"night"];
//                [self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];
            } else {
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"night"];
//                [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:@"change" object:nil];
            [UIApplication sharedApplication].statusBarStyle = NSUSERDEFAULTS_NIGHT;
        }
    };
    _bottomView.block = bottomButtonBlock;
    [self.view addSubview:_bottomView];
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //获得当前页面item的indexPath.row
    _index = indexPath.row;
    WFChannelDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"back" forIndexPath:indexPath];
    if (_dataSourceArray.count != 0) {
        cell.dataArray = _dataSourceArray[indexPath.row];
    }
    cell.delegate = self;
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    //数据安全过滤
    if ([self getIndexFromLocation:location] < 6) {
        //取出数组中对应model
        WFChannelDetailModel *model = [_dataSourceArray[_index] objectAtIndex:[self getIndexFromLocation:location]];
        WFChannelWebViewController *wc = [[WFChannelWebViewController alloc]init];
        wc.model = model;
        wc.preferredContentSize = CGSizeMake(0, 500);
        return wc;
    }
    return nil;
}

- (NSInteger)getIndexFromLocation:(CGPoint)location
{
    //判断数组是否有值
    if (_dataSourceArray.count) {
        
        WFChannelDetailModel *model = [_dataSourceArray[_index] objectAtIndex:0];
        //判断model是否为空
        if (model) {
            //判断图片数组是否为空, 不空使用image布局, 空则使用label布局
            if ([model media].count)
            {
                if (location.y < 330 * FITHEIGHT && location.y > 120 * FITHEIGHT)
                {
                    return 0;
                }
                else if(location.y > (330 + (667 - 370) / 3 * 2) * FITHEIGHT && location.y < SCREEN_HEIGHT - 40)
                {
                    return 5;
                }
                else if(location.y > (330 + (667 - 370) / 3) * FITHEIGHT && location.y < (330 + (667 - 370) / 3 * 2) * FITHEIGHT)
                {
                    if (location.x < SCREEN_WIDTH / 2) {
                        return 3;
                    }
                    else
                    {
                        return 4;
                    }
                }
                else if(location.y > 330 * FITHEIGHT && location.y < (330 + (667 - 370) / 3) * FITHEIGHT)
                {
                    if (location.x < SCREEN_WIDTH / 2) {
                        return 1;
                    }
                    else
                    {
                        return 2;
                    }
                }
            }
            else
            {
                if (location.y > 120 * FITHEIGHT && location.y < (120 + 517 / 4) * FITHEIGHT) {
                    return 0;
                }
                else if(location.y > (637 - 517 / 4) * FITHEIGHT && location.y < SCREEN_HEIGHT - 40)
                {
                    return 5;
                }
                else if(location.y > (120 + 517 / 4) * FITHEIGHT && location.y < (120 + 517 / 2) * FITHEIGHT)
                {
                    if (location.x < SCREEN_WIDTH / 2) {
                        return 1;
                    }
                    else
                    {
                        return 2;
                    }
                }
                else if(location.y > (120 + 517 / 2) * FITHEIGHT && location.y < (120 + 517 / 4 * 3) * FITHEIGHT)
                {
                    if (location.x < SCREEN_WIDTH / 2) {
                        return 3;
                    }
                    else
                    {
                        return 4;
                    }
                }
            }
        }
    }
    //返回一个废弃值
    return 10;
}


- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
    [self showViewController:viewControllerToCommit sender:self];
}

//- (void)createHeaderView
//{
//    BaseClassImageView *imageView = [[BaseClassImageView  alloc]initWithFrame:CGRectMake(127.5, -20, 120, 120)];
//    imageView.contentMode = 0;
//    imageView.clipsToBounds = 0;
//    [_backCollectionView addSubview:imageView];
//    [imageView sd_setImageWithURL:[NSURL URLWithString:[[WFModelSingleton shareInstance].model.block_info objectForKey:@"pic"]] placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//        imageView.image = [image imageWithColor:[UIColor RGBFromHexColor:[[WFModelSingleton shareInstance].model.block_info objectForKey:@"block_color"]]];
//    }];
//    for (NSInteger i = 0; i < 2; i++) {
//        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20 + 205 * i, 63, 130, 1)];
//        view.backgroundColor = [UIColor blackColor];
//        [self.view addSubview:view];
//    }
//}
//- (UIStatusBarStyle)preferredStatusBarStyle
//{
//    if (NSUSERDEFAULTS_NIGHT) {
//        return UIStatusBarStyleLightContent;
//    } else {
//        return UIStatusBarStyleDefault;
//    }
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
